<p>Add, Remove, Enable, Disable, Sort and Configure data sources as appropriate for your situation.</p>
<p>Select which data source(s) to use for your lookups, and the order in which you want them used:</p>
<div id="CIDSourcesList">
    <script language="javascript">
        $(document).ready(function() {
            $('#superfecta_options').submit(function() { 
                $(this).ajaxSubmit(); 
                alert("Saved!");
                return false; 
            });
            $("#update_check").change(function() {
                if ($(this).is (':checked'))    {
                    $('#sources tr').each(function(index) {
                        var id = $(this).attr("id");
                        if(id != 'title_row') {
                            $('#' + id + '_update').show();
                        }
                    });
                } else {
                    $('#sources tr').each(function(index) {
                        var id = $(this).attr("id");
                        if(id != 'title_row') {
                            $('#' + id + '_update').hide();
                        }
                    });
                }
            });
            //Show/Hide DID Test Box
            if($("#DID").val().length > 0) {
                $("#didbox").show();
            } else {
                $("#didbox").hide();
            }
            
            //Show/Hide DID Test Box on DID value change greater than 0
            $("#DID").change(function(){
                if($(this).val().length > 0) {
                    $("#didbox").fadeIn('slow');
                } else {
                    $("#didbox").fadeOut('slow');
                }
            })
            $('#thenumber').keypress(function(e) {
                if(e.which == 13) {
                    jQuery(this).blur();
                    jQuery('#submit').focus();
                    superfecta_debug_go();
                }
            });
            $('#thedid').keypress(function(e) {
                if(e.which == 13) {
                    jQuery(this).blur();
                    jQuery('#submit').focus();
                    superfecta_debug_go();
                }
            });
        });
        
        function superfecta_debug_go() {
            var debuglevel = $('#debug_level').val();
            var thenumber = $('#thenumber').val();
            var thedid = $('#thedid').val();
            var scheme = '{$scheme}';
            if($('#allscheme').is (':checked')) {
                scheme = 'ALL';
            }
            $('#superfecta_debug_display').html('<div id="debug" style="background-color: #E0E0E0; width:70%; margin-left:auto;margin-right:auto;"><img src="assets/superfecta/images/loading.gif" /></div>');
            $.ajaxSetup({ cache: false });
            $.get('config.php?quietmode=1&handler=file&module=superfecta&file=callerid.html.php&scheme='+scheme+'&thenumber='+thenumber+'&debug='+debuglevel+'&thedid='+thedid, function(data) {
                $('#superfecta_debug_display').html('<div id="debug" style="background-color: #E0E0E0; width:100%">'+data+'</div>');
            });
        }
        
        function moveup_source(id) {
            //This is suprisingly the BEST movement jquery method I have found! EVER
            var parent_id = $('#' + id).parent().parent().attr("id");
            var row = $('#' + id).parents("tr:first");
            $('#' + parent_id).fadeOut('slow', function() {
                row.insertBefore(row.prev());
                $('#' + parent_id).fadeIn('slow');
                source_order();
            })
        }
        function movedown_source(id) {
            //This is suprisingly the BEST movement jquery method I have found! EVER
            var parent_id = $('#' + id).parent().parent().attr("id");
            var row = $('#' + id).parents("tr:first");
            $('#' + parent_id).fadeOut('slow', function() {
                row.insertAfter(row.next());
                $('#' + parent_id).fadeIn('slow');
                source_order();
            })
        }
        
        //Fix graphics display and send content
        function source_order() {
            //Get order here and then re-do all of the gfx to make sense again to the gui
            var total = $('#sources tr').size();
            var source_json="[";
            $('#sources tr').each(function(index) {
                var id = $(this).attr("id");
                if(($(this).attr("id") != 'title_row') && $('#'+id+'_enabled').is(':checked')) {
                    if(index == 1) {
                        $('#' + id + '_moveup').hide();
                        var nextid = $(this).next().attr('id');
                        if($('#'+nextid+'_enabled').is(':checked')) {
                            source_json = source_json + '"'+ id +'",'
                            $('#' + id + '_movedown').show();
                        } else {
                            source_json = source_json + '"'+ id +'"'
                            $('#' + id + '_movedown').hide();
                        }
                    } else {
                        var nextid = $(this).next().attr('id');
                        if($('#'+nextid+'_enabled').is(':checked')) {
                            $('#' + id + '_movedown').show();
                            source_json = source_json + '"'+ id +'",'
                        } else {
                            $('#' + id + '_movedown').hide();
                            source_json = source_json + '"'+ id +'"'
                        }
                        $('#' + id + '_moveup').show();
                    }
                }
            });
            source_json = source_json + "]";
            $.ajaxSetup({ cache: false });
            $.getJSON("config.php?quietmode=1&handler=file&module=superfecta&file=ajax.html.php&type=update_sources&scheme={$scheme}", 
            {
                data: source_json
            },
            function(json) {

            });
        }
        
        function disable_source(id) {
            var parent_id = $('#' + id).parent().parent().attr("id");
            $('#' + parent_id + '_moveup').fadeOut('slow');
            $('#' + parent_id + '_movedown').fadeOut('slow');
            $('#sources tr').each(function(index) {
                var eid = $(this).attr("id");
                if(($(this).attr("id") != 'title_row') && !$('#'+eid+'_enabled').is(':checked') && parent_id != eid) {
                    $('#' + parent_id).insertBefore($(this));
                    source_order();
                    return false;
                }
            });
            
        }
        
        function enable_source(id) {
            var parent_id = $('#' + id).parent().parent().attr("id");
            $('#' + parent_id + '_moveup').fadeIn('slow');
            $('#' + parent_id + '_movedown').fadeIn('slow');
            $('#sources tr').each(function(index) {
                var eid = $(this).attr("id");
                if(($(this).attr("id") != 'title_row') && !$('#'+eid+'_enabled').is(':checked') && parent_id != eid) {
                    $('#' + parent_id).insertBefore($(this));
                    source_order();
                    return false;
                }
            });
        }
        
        function options(id) {
            var parent_id = $('#' + id).parent().parent().attr("id");
            $('#options').fadeOut('slow', function() {
                $.ajaxSetup({ cache: false });
                $.getJSON("config.php?quietmode=1&handler=file&module=superfecta&file=ajax.html.php&type=options&scheme={$scheme}&source="+parent_id, function(json) {
                    if(json.success && json.show) {
                        $('#options').fadeIn('slow').html(json.data);
						//We must re-run this everytime we reload a source's options.
						$("a.info").each(function(){$(this).after('<span class="help">?<span>'+$(this).find('span').html()+'</span></span>');$(this).find('span').remove();$(this).replaceWith($(this).html())})
                        $(".help").on('mouseenter',function(){side=ipbx.conf.text_dir=='lrt'?'left':'right';var pos=$(this).offset();var offset=(200-pos.side)+"px";$(this).find("span").css(side,offset).stop(true,true).delay(500).animate({opacity:"show"},750);}).on('mouseleave',function(){$(this).find("span").stop(true,true).animate({opacity:"hide"},"fast");});
						$('#form_options_'+parent_id).submit(function() { 
                            $(this).ajaxSubmit(); 
                            alert("Saved!");
                            return false; 
                        });
                    }
                });
            });
        }
    </script>
    <input type="hidden" name="src_up" value="">
    <input type="hidden" name="src_down" value="">
    <input type="hidden" name="selected_source" value="">
    <input type="hidden" name="update_file" value="">
    <input type="hidden" name="delete_file" value="">
    <input type="hidden" name="revert_file" value="">
    
    <table>
        <tr>
            <td>
                <table border="0" id="sources" cellspacing="0" cellpadding="2">
                    <tr id="title_row">
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td><strong>Data Source Name</strong></td>
                        <td align="center"><strong>Disabled</strong></td>
                        <td align="center"><strong>Enabled</strong></td>
                    </tr>
                    {loop="sources"}
                    <tr id="{$value.source_name}" class="{$value.status}">
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td><img id="{$value.source_name}_movedown" onclick="movedown_source(this.id);" src="images/scrolldown.gif" {if condition="$value.showdown === FALSE"}hidden{/if}/></td>
                        <td><img id="{$value.source_name}_moveup" onclick="moveup_source(this.id);" src="images/scrollup.gif" {if condition="$value.showup === FALSE"}hidden{/if}/></td>
                        <td>{if condition="$value.show_link === TRUE"}<a id="{$value.source_name}_options" onclick="options(this.id)" style="cursor:pointer;">{/if}{$value.pretty_source_name}{if condition="$value.show_link === TRUE"}</a>{/if}<a class="info"><span>{$value.description}</span></a><img id="{$value.source_name}_update" onclick="movedown_update(this.id);" src="assets/superfecta/images/update.png" {if condition="$value.showupdate === FALSE"}hidden{/if}/></td>
                        <td align="center"><input type="radio" id="{$value.source_name}_disabled" name="{$value.source_name}" value="disabled" onclick="disable_source(this.id)" {if condition="$value.enabled === FALSE"}checked{/if}/></td>
                        <td align="center"><input type="radio" id="{$value.source_name}_enabled" name="{$value.source_name}" value="enabled" onclick="enable_source(this.id)" {if condition="$value.enabled === TRUE"}checked{/if}/></td>
                    </tr>
                    {/loop}
                </table>
            </td>
            <td valign="top">
                <div id="options" style="background: #C0C0C0"></div>
            </td>
        </tr>
    </table>

</div>
<br><br>
<table border="0">
    <tr>
        <td valign="top">
            <form method="POST" action="config.php?quietmode=1&handler=file&module=superfecta&file=ajax.html.php&type=update_scheme" name="superfecta_options" id="superfecta_options">
                <input type="hidden" name="scheme_name_orig" value="{$scheme_name}">
                <table border="0" id="table1" cellspacing="1">
                    <tr>
                        <td><a href="javascript:return(false);" class="info"><strong>Scheme Name:</strong><span>Duplicate Scheme names not allowed.</span></a></td>
                        <td><input type="text" name="scheme_name" size="23" maxlength="20" value="{$scheme_name}"></td>
                    </tr>
                    <tr>
                        <td colspan="2"><font face="Arial"><br><u>General Options</font></u></td>
                    </tr>


                    <tr>
                        <td valign="top"><a href="javascript:return(false);" class="info">DID Rules<span>Define the expected DID Number if your trunk passes DID on incoming calls. <br><br>Leave this blank to match calls with any or no DID info.<br><br>This rule trys both absolute and pattern matching (eg "_2[345]X", to match a range of numbers). (The "_" underscore is optional.)</span></a>:</td>
                        <td>
                            <textarea id="DID" tabindex="1" cols="20" rows="5" name="DID">{$did}</textarea>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top">
                            <a href="javascript:return(false);" class="info">CID Rules<span>Incoming calls with CID matching the patterns specified here will use this CID Scheme. If this is left blank, this scheme will be used for any CID. It can be used to add or remove prefixes.<br>
                                    <strong>Many sources require a specific number of digits in the phone number. It is recommended that you use the patterns to remove excess country code data from incoming CID to increase the effectiveness of this module.</strong><br>
                                    Note that a pattern without a + or | (to add or remove a prefix) will not make any changes but will create a match. Only the first matched pattern will be executed and the remaining rules will not be acted on.<br /><br /><b>Rules:</b><br />
                                    <strong>X</strong>&nbsp;&nbsp;&nbsp; matches any digit from 0-9<br />
                                    <strong>Z</strong>&nbsp;&nbsp;&nbsp; matches any digit from 1-9<br />
                                    <strong>N</strong>&nbsp;&nbsp;&nbsp; matches any digit from 2-9<br />
                                    <strong>[1237-9]</strong>&nbsp;   matches any digit or letter in the brackets (in this example, 1,2,3,7,8,9)<br />
                                    <strong>.</strong>&nbsp;&nbsp;&nbsp; wildcard, matches one or more characters (not allowed before a | or +)<br />
                                    <strong>|</strong>&nbsp;&nbsp;&nbsp; removes a dialing prefix from the number (for example, 613|NXXXXXX would match when some one dialed "6135551234" but would only pass "5551234" to the Superfecta look up.)<br><strong>+</strong>&nbsp;&nbsp;&nbsp; adds a dialing prefix to the number (for example, 1613+NXXXXXX would match when someone dialed "5551234" and would pass "16135551234" to the Superfecta look up.)<br /><br />
                                    You can also use both + and |, for example: 01+0|1ZXXXXXXXXX would match "016065551234" and dial it as "0116065551234" Note that the order does not matter, eg. 0|01+1ZXXXXXXXXX does the same thing.</span></a>:
                        </td>
                        <td valign="top">
                            <textarea tabindex="2" id="dialrules" cols="20" rows="5" name="CID_rules">{$cid_rules}</textarea>
                        </td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td><a href="javascript:return(false);" class="info">Lookup Timeout<span>Specify a timeout in seconds for each source. If the source fails to return a result within the alloted time, the script will move on.</span></a></td>
                        <td><input type="text" name="Curl_Timeout" size="4" maxlength="5" value="{$curl_timeout}"></td>
                    </tr>
                    <tr>
                        <td>
                            <a href="javascript:return(false);" class="info">Superfecta Processor
                                <span>These are the types of Superfecta Processors:<br />
                                    {loop="processors_list"}
                                    <strong>{$value.name}:</strong> {$value.description}<br />
                                    {/loop}
                                </span></a>
                        </td>
                        <td>
                            <select name="processor">
                                {loop="processors_list"}
                                <option value='{$value.filename}' {if condition="$value.selected === TRUE"}selected{/if}>{$value.name}</option>
                                {/loop}
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td><a href="javascript:return(false);" class="info">Multifecta Timeout<span>Specify a timeout in seconds defining how long multifecta will obey the source priority. After this timeout, the first source to respond with a CNAM will be taken, until "Lookup Timeout" is reached.</span></a></td>
                        <td><input type="text" name="multifecta_timeout" size="4" maxlength="5" value="{$multifecta_timeout}"></td>
                    </tr>
                    <tr>
                        <td><a href="javascript:return(false);" class="info">CID Prefix URL<span>If you wish to prefix information on the caller id you can specify a url here where that prefix can be retrieved.<br>The data will not be parsed in any way, and will be truncated to the first 10 characters.<br>Example URL: http://www.example.com/GetCID.php?phone_number=[thenumber]<br>[thenumber] will be replaced with the full 10 digit phone number when the URL is called.</span></a></td>
                        <td><input type="text" name="Prefix_URL" size="23" maxlength="255" value="{$prefix_url}"></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td><a href="javascript:return(false);" class="info">SPAM Text<span>This text will be prepended to Caller ID information to help you identify calls as SPAM calls.</span></a></td>
                        <td><input type="text" name="SPAM_Text" size="23" maxlength="20" value="{$spam_text}"></td>
                    </tr>
                    <tr>
                        <td><a href="javascript:return(false);" class="info">SPAM Text Substituted<span>When enabled, the text entered in "SPAM Text" (above) will replace the CID completely rather than pre-pending the CID value.</span></a></td>
                        <td>
                            <input type="checkbox" name="SPAM_Text_Substitute" value="Y" {if condition="$spam_text_substitute === TRUE"}checked{/if}>
                        </td>
                    </tr>
                    <tr>
                        <td><a href="javascript:return(false);" class="info">Enable SPAM Interception<span>When enabled, Spam calls can be diverted or terminated.</span></a></td>
                        <td>
                            <input type="checkbox" onclick="toggleInterceptor()" name="enable_interceptor" value="Y" {if condition="$spam_int === TRUE"}checked{/if}>
                        </td>
                    </tr>
                </table>
                <table id="InterceptorVector" border="0">
                    <tr>
                        <td><a href="javascript:return(false);" class="info">SPAM Send Threshold<span>This is the threshold to send the call to the specified destination below</span></a></td>
                        <td><input type="text" name="SPAM_threshold" size="4" maxlength="2" value="{$spam_threshold}"></td>
                    </tr>
                    <tr class="incerceptorCell">
                        <td colspan="2">Send Spam Call To:</td>
                    </tr>
                    <tr class="incerceptorCell">
                        <td colspan="2">{$interceptor_select}</td>
                    </tr>
                </table>
                <p><a target="_blank" href="modules/superfecta/disclaimer.html">(License Terms)&nbsp; </a><input type="submit" value="Agree and Save" name="Save"></p>
                <p style="font-size:12px;">(* By clicking on either the &quot;Agree and Save&quot;<br>button, or the &quot;Debug&quot; button on this form<br>you are agreeing to the Caller ID Superfecta<br>Licensing Terms.)</p>
            </form>
        </td>				
        <td valign="top">
            <form name="superfecta_debug" onsubmit="return false">
                <p><u>Test a phone number against the selected sources.</u><br>
                    <span id="didbox"><a href="javascript:return(false);" class="info">DID Number:<span>The DID to test this scheme against.</span></a> <input type="text" id="thedid" size="15" maxlength="20" name="thedid"><br></span>
                    <a href="javascript:return(false);" class="info">Phone Number:<span>Phone number to test this scheme against.</span></a> <input type="text" id="thenumber" size="15" maxlength="20" name="thenumber"><input type="button" value="Debug" onclick="superfecta_debug_go();"><br>
                    <font size=2><input type="checkbox" name="allscheme" id="allscheme" value="All">
                    <a href="javascript:return(false);" class="info">Test all CID schemes<span>When enabled, the debug function will test the number entered against all of the configured CID schemes.<br>When disabled, debug only checks up to the first scheme that provides positive results.</span></a> <br/>
                    Debug Level:<select name="debug" id="debug_level"><option value="0">NONE</option><option value="1" selected>INFO</option><option value="2">WARN</option><option value="3">ALL</option></select></font></p>
            </form>
            <div id="superfecta_debug_display" style="background-color: #E0E0E0; width:100%"></div>
        </td>
    </tr>
</table>
<script language="javascript">
    //$.get('ajax/test.html', function(data) {
    //$('.result').html(data);
    //alert('Load was performed.');
    //});

    
    function reset_infoboxes(){
        body_loaded();
        // test for a function that seems to only be in issabelpbx 2.8+
        if(typeof window.tabberAutomaticOnLoad == 'function') {
            $("a.info").hover(function () {
                var pos = $(this).offset();
                var left = (200 - pos.left) + "px";
                $(this).find("span").css("left", left).stop(true, true).delay(500).animate({
                    opacity: "show"
                }, 750);
            }, function () {
                $(this).find("span").stop(true, true).animate({
                    opacity: "hide"
                }, "fast");
            });
        }
    }

    function toggleInterceptor() {
	
        var row = document.getElementById("InterceptorVector");
        if(document.Superfecta.enable_interceptor.checked && row)
        {
            row.style.display = '';
        }
        else if(row)
        {
            row.style.display = 'none';
        }
    }
</script>
